package com.cskaoyan.service;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuqiushi
 * @since 2022/09/06 18:29
 */
@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketIssueMapper marketIssueMapper;
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Override
    public AdminGoodsListVO getGoodsList(AdminGoodsListBO adminGoodsListBO) {
        //首先利用mapper层查询
        //先做分页
        PageHelper.startPage(adminGoodsListBO.getPage(), adminGoodsListBO.getLimit());
        //利用ByExample做查询
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        //设置排序
        marketGoodsExample.setOrderByClause(adminGoodsListBO.getSort() + " " + adminGoodsListBO.getOrder());
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (adminGoodsListBO.getGoodsSn() != null) {
            criteria.andGoodsSnEqualTo(adminGoodsListBO.getGoodsSn());
        }
        if (adminGoodsListBO.getGoodsId() != null) {
            criteria.andIdEqualTo(adminGoodsListBO.getGoodsId());
        }
        if (!StringUtil.isEmpty(adminGoodsListBO.getName())) {
            criteria.andNameLike("%"+adminGoodsListBO.getName()+"%");
        }

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        //获取pages,total等
        PageInfo<MarketGoods> goodsPageInfo = new PageInfo<>(marketGoods);
        int pages = goodsPageInfo.getPages();
        long total = goodsPageInfo.getTotal();
        //将查询的结果封装
        AdminGoodsListVO adminGoodsListVO = new AdminGoodsListVO();
        adminGoodsListVO.setLimit(adminGoodsListBO.getLimit());
        adminGoodsListVO.setPage(adminGoodsListBO.getPage());
        adminGoodsListVO.setPages(pages);
        adminGoodsListVO.setTotal((int) total);
        //这里需要做一个typeHandler，将String型的gallery转化为List<String>
        adminGoodsListVO.setList(marketGoods);
        PageHelper.clearPage();
        return adminGoodsListVO;
    }

    @Override
    public AdminGoodsCatAndBrandVO getCatAndBrand() {
        AdminGoodsCatAndBrandVO catAndBrandVO = new AdminGoodsCatAndBrandVO();
        //利用mapper查询
        //先查cat
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        LinkedList<MarketCategory> marketCategories2 = new LinkedList<>(marketCategories);
        //再查brand
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        //先封装cat
        //先跳出所有大类
        List<MarketCategory> parentCats = marketCategories.stream().filter(categroy -> categroy
                .getPid() == 0).collect(Collectors.toList());
        List<AdminGoodsCatAndBrandVO.CategoryListDTO> categoryListDTOs =
                new LinkedList<>();
        for (MarketCategory parentCat : parentCats) {
            AdminGoodsCatAndBrandVO.CategoryListDTO categoryListDTO = new AdminGoodsCatAndBrandVO.CategoryListDTO();
            //设置id
            categoryListDTO.setValue(parentCat.getId());
            //设置name
            categoryListDTO.setLabel(parentCat.getName());
            List<AdminGoodsCatAndBrandVO.CategoryListDTO.ChildrenDTO> childListDTO =
                    new LinkedList<>();
            //挑出该大类的所有子类
            List<MarketCategory> childCollect =
                    marketCategories2.stream().filter(a -> a.getPid().equals(parentCat.getId())).collect(Collectors.toList());
            for (MarketCategory child : childCollect) {
                AdminGoodsCatAndBrandVO.CategoryListDTO.ChildrenDTO childrenDTO =
                        new AdminGoodsCatAndBrandVO.CategoryListDTO.ChildrenDTO();
                childrenDTO.setValue(child.getId());
                childrenDTO.setLabel(child.getName());
                childListDTO.add(childrenDTO);
            }
            //设置child
            categoryListDTO.setChildren(childListDTO);
            //加入到链表里
            categoryListDTOs.add(categoryListDTO);
        }
        //给category赋值
        catAndBrandVO.setCategoryList(categoryListDTOs);
        //再封装brand
        LinkedList<AdminGoodsCatAndBrandVO.BrandListDTO> brandListDTOS = new LinkedList<>();
        for (MarketBrand marketBrand : marketBrands) {
            AdminGoodsCatAndBrandVO.BrandListDTO brandListDTO = new AdminGoodsCatAndBrandVO.BrandListDTO();
            brandListDTO.setValue(marketBrand.getId());
            brandListDTO.setLabel(marketBrand.getName());
            brandListDTOS.add(brandListDTO);
        }
        //给brand赋值
        catAndBrandVO.setBrandList(brandListDTOS);
        //封装，返回
        return catAndBrandVO;
    }

    //新增商品
    @Transactional
    @Override
    public int create(AdminGoodsCreateBO createBO) {
        AdminGoodsCreateBO.GoodsDTO newGoods = createBO.getGoods();
        Date date = new Date(System.currentTimeMillis());
        //用一个try-catch包裹所有操作，使所有插入称为一个原子操作
        try {
            //首先插入goods表
            MarketGoods marketGoods = getMarketGoodsByGoodsCreateBO(newGoods);
            marketGoods.setAddTime(date);
            marketGoods.setUpdateTime(date);
            marketGoodsMapper.insertSelective(marketGoods);
            //再插入spec表
            List<AdminGoodsCreateBO.SpecificationsDTO> specifications = createBO.getSpecifications();
            for (AdminGoodsCreateBO.SpecificationsDTO specification : specifications) {
                MarketGoodsSpecification templeSpec = getGoodsSpecificationByGoodsCreateBO(marketGoods, specification);
                templeSpec.setAddTime(date);
                templeSpec.setUpdateTime(date);
                //最后将其插入到表中
                marketGoodsSpecificationMapper.insert(templeSpec);
            }
            //再插入product表
            List<AdminGoodsCreateBO.ProductsDTO> products = createBO.getProducts();
            for (AdminGoodsCreateBO.ProductsDTO product : products) {
                MarketGoodsProduct marketGoodsProduct = getMarketGoodsProductByGoodsCreateBO(marketGoods, product);
                marketGoodsProduct.setAddTime(date);
                marketGoodsProduct.setUpdateTime(date);
                //插入
                marketGoodsProductMapper.insertSelective(marketGoodsProduct);
            }
            //再插入attributes表
            List<AdminGoodsCreateBO.AttributesDTO> attributes = createBO.getAttributes();
            for (AdminGoodsCreateBO.AttributesDTO attribute : attributes) {
                MarketGoodsAttribute marketGoodsAttribute = getMarketGoodsAttributeByGoodsCreateBO(marketGoods,
                        attribute);
                marketGoodsAttribute.setAddTime(date);
                marketGoodsAttribute.setUpdateTime(date);
                //插入
                marketGoodsAttributeMapper.insertSelective(marketGoodsAttribute);
            }
            //再插入关键字表
            MarketKeyword marketKeyword = new MarketKeyword();
            marketKeyword.setKeyword(createBO.getGoods().getKeywords());
            marketKeyword.setAddTime(date);
            marketKeyword.setUpdateTime(date);
            marketKeyword.setDeleted(false);
            marketKeywordMapper.insertSelective(marketKeyword);
        } catch (NumberFormatException | NullPointerException e1) {
            e1.printStackTrace();
            //有异常直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 401;
        } catch (Exception e) {
            e.printStackTrace();
            //有异常直接回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 10000;
        }
        return 0;
    }

    @Override
    public AdminGoodsDetailVO getGoodsDetail(Integer id) {
        //先查goods
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        //将goodsId存在list里方便mapper用
        LinkedList<Integer> goodsIdList = new LinkedList<>();
        goodsIdList.add(marketGoods.getId());
        //再调用select查询categoryIds,一个大的品类有小品类
        Integer categoryId = marketGoods.getCategoryId();
        MarketCategory category = marketCategoryMapper.selectByPrimaryKey(categoryId);
        Integer parentCategory = category.getPid();
        LinkedList<Integer> categoryIds = new LinkedList<>();
        categoryIds.add(categoryId);
        categoryIds.add(parentCategory);
        //再查attributes
        MarketGoodsAttributeExample goodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = goodsAttributeExample.createCriteria();
        criteria.andGoodsIdIn(goodsIdList);
        List<MarketGoodsAttribute> marketGoodsAttributes =
                marketGoodsAttributeMapper.selectByExample(goodsAttributeExample);
        //再查specifications
        MarketGoodsSpecificationExample goodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria1 = goodsSpecificationExample.createCriteria();
        criteria1.andGoodsIdIn(goodsIdList);
        List<MarketGoodsSpecification> marketGoodsSpecifications =
                marketGoodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        //再查products
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = marketGoodsProductExample.createCriteria();
        criteria2.andGoodsIdIn(goodsIdList);
        List<MarketGoodsProduct> marketGoodsProducts =
                marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
        //利用set为其赋值
        AdminGoodsDetailVO result = new AdminGoodsDetailVO();
        result.setGoods(marketGoods);
        result.setAttributes(marketGoodsAttributes);
        result.setCategoryIds(categoryIds);
        result.setProducts(marketGoodsProducts);
        result.setSpecifications(marketGoodsSpecifications);

        return result;
    }

    @Transactional
    @Override
    public int update(AdminGoodsUpdateBO updateBO) {
        try {
            //首先修改goods表
            MarketGoods updateBOGoods = updateBO.getGoods();
            marketGoodsMapper.updateByPrimaryKey(updateBOGoods);
            //再修改specification
            List<MarketGoodsSpecification> specifications = updateBO.getSpecifications();
            for (MarketGoodsSpecification specification : specifications) {
                marketGoodsSpecificationMapper.updateByPrimaryKey(specification);
            }
            //再修改products
            List<MarketGoodsProduct> products = updateBO.getProducts();
            for (MarketGoodsProduct product : products) {
                marketGoodsProductMapper.updateByPrimaryKey(product);
            }
            //再修改attributes
            List<MarketGoodsAttribute> attributes = updateBO.getAttributes();
            for (MarketGoodsAttribute attribute : attributes) {
                marketGoodsAttributeMapper.updateByPrimaryKey(attribute);
            }
        } catch (Exception e) {
            //出现异常rollback
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return 402;
        }
        return 0;
    }

    //删除商品
    @Override
    public int delete(AdminGoodsDeleteBO adminGoodsDeleteBO) {
        //将各个表中的deleted置为true即为删除
        try {
            //将goodsId存在list里方便mapper用
            LinkedList<Integer> goodsIdList = new LinkedList<>();
            goodsIdList.add(adminGoodsDeleteBO.getId());
            //首先修改goods表中的数据
            MarketGoods marketGoods = new MarketGoods();
            marketGoods.setId(adminGoodsDeleteBO.getId());
            marketGoods.setDeleted(true);
            marketGoodsMapper.updateByPrimaryKeySelective(marketGoods);
            //再修改specification
            MarketGoodsSpecificationExample goodsSpecificationExample = new MarketGoodsSpecificationExample();
            goodsSpecificationExample.createCriteria().andGoodsIdIn(goodsIdList);
            MarketGoodsSpecification marketGoodsSpecification = new MarketGoodsSpecification();
            //设置为已删除
            marketGoodsSpecification.setDeleted(true);
            marketGoodsSpecificationMapper.updateByExampleSelective(marketGoodsSpecification,
                    goodsSpecificationExample);
            //再修改products
            MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
            goodsProductExample.createCriteria().andGoodsIdIn(goodsIdList);
            MarketGoodsProduct marketGoodsProduct = new MarketGoodsProduct();
            //设置为删除
            marketGoodsProduct.setDeleted(true);
            marketGoodsProductMapper.updateByExampleSelective(marketGoodsProduct, goodsProductExample);
            //再修改attributes
            MarketGoodsAttributeExample goodsAttributeExample = new MarketGoodsAttributeExample();
            goodsAttributeExample.createCriteria().andGoodsIdIn(goodsIdList);
            MarketGoodsAttribute marketGoodsAttribute = new MarketGoodsAttribute();
            //设置为删除
            marketGoodsAttribute.setDeleted(true);
            marketGoodsAttributeMapper.updateByExampleSelective(marketGoodsAttribute, goodsAttributeExample);
        } catch (Exception e) {
            //出现异常rollback
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return 402;
        }
        return 0;
    }

    @Override
    public Integer getWXGoodsCount() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        return marketGoods.size();
    }

    @Override
    public WXGoodsCategoryVo getWXGoodsCategory(Integer id) {
        WXGoodsCategoryVo categoryVo = new WXGoodsCategoryVo();

        //打开当前的类目，其pid不能为0，为0则打开其子类第一个
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);

        if (marketCategory.getPid() == 0) {
            //其pid为0则打开子类第一个（先找子类表）
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            //将其id作为子类pid
            marketCategoryExample.createCriteria().andPidEqualTo(id);
            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);

            //由于pid为0，则选自己子类第一个作为当前类
            categoryVo.setCurrentCategoryEntity(marketCategories.get(0));
            for (MarketCategory category : marketCategories) {
                categoryVo.setBrotherCategoryEntity(category);
            }

            //由于pid为0，则自己就是父类
            categoryVo.setParentCategoryEntity(marketCategory);
        } else {
            //由于pid不为0,则自己就是当前类
            categoryVo.setCurrentCategoryEntity(marketCategory);

            //其pid不为0，将其pid作为兄弟类的pid
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            marketCategoryExample.createCriteria().andPidEqualTo(marketCategory.getPid());
            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
            for (MarketCategory category : marketCategories) {
                categoryVo.setBrotherCategoryEntity(category);
            }

            //用pid作为id查找父类
            MarketCategory marketCategoryParent = marketCategoryMapper.selectByPrimaryKey(marketCategory.getPid());
            categoryVo.setParentCategoryEntity(marketCategoryParent);
        }

        return categoryVo;
    }

    @Override
    public WXGoodsListVo getWXGoodsList(WxGoodsListBO wxGoodsListBO) {
        PageHelper.startPage(wxGoodsListBO.getPage(), wxGoodsListBO.getLimit());
        WXGoodsListVo goodsListVo = new WXGoodsListVo();
        goodsListVo.setPage(wxGoodsListBO.getPage());
        goodsListVo.setLimit(wxGoodsListBO.getLimit());
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        if (wxGoodsListBO.getOrder()!=null) {
            //做分页
            marketGoodsExample.setOrderByClause(wxGoodsListBO.getSort()+" "+wxGoodsListBO.getOrder());
        }
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (wxGoodsListBO.getIsHot()!=null) {
            criteria.andIsHotEqualTo(wxGoodsListBO.getIsHot());
        }
        if (wxGoodsListBO.getIsNew()!=null) {
            criteria.andIsHotEqualTo(wxGoodsListBO.getIsNew());
        }
        if (wxGoodsListBO.getCategoryId()!=null&& wxGoodsListBO.getCategoryId() !=0) {
            criteria.andCategoryIdEqualTo(wxGoodsListBO.getCategoryId());
        }
        if (wxGoodsListBO.getBrandId()!=null) {
            criteria.andBrandIdEqualTo(wxGoodsListBO.getBrandId());
        }
        if(wxGoodsListBO.getKeyword()!= null){
            criteria.andNameLike("%"+wxGoodsListBO.getKeyword()+"%");
        }
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExample);
        PageInfo pageInfo = new PageInfo(marketGoods);
        goodsListVo.setPages(pageInfo.getPages());
        goodsListVo.setTotal((int) pageInfo.getTotal());

        for (MarketGoods goods : marketGoods) {
            goodsListVo.setListEntity(goods);
        }
        return goodsListVo;
    }

    @Override
    public WXGoodsDetailVo getWXGoodsDetail(Integer id) {
        //TODO 添加历史足迹
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser info = (MarketUser) session.getAttribute("info");
        if(info!=null && info.getId()!=null){
            //判重
            MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
            MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
            criteria.andUserIdEqualTo(info.getId());
            criteria.andGoodsIdEqualTo(id);
            List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
            if(marketFootprints.size()!=0){
                Date date = new Date();
                marketFootprints.get(0).setUpdateTime(date);
                marketFootprints.get(0).setAddTime(date);
                marketFootprintMapper.updateByPrimaryKey(marketFootprints.get(0));
            }else {
                MarketFootprint footprint = new MarketFootprint();
                Date date = new Date();
                footprint.setUserId(info.getId());
                footprint.setGoodsId(id);
                footprint.setAddTime(date);
                footprint.setUpdateTime(date);
                footprint.setDeleted(false);
                marketFootprintMapper.insert(footprint);
            }


        }

        WXGoodsDetailVo detailVo = new WXGoodsDetailVo();
        //赋值SpecificationListEntity
        //先以goodsId查到所有符合的规格
        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
        specificationExample.createCriteria().andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(specificationExample);
        //从该list里面根据名字不同分成不同的表
        String name = specifications.get(0).getSpecification();
        List<MarketGoodsSpecification> list = new ArrayList<>();
        for (MarketGoodsSpecification specification : specifications) {
            if(name.equals(specification.getSpecification())){
                list.add(specification);
            }else {
                detailVo.setSpecificationEntity(list,name);
                list.clear();
                name = specification.getSpecification();
                list.add(specification);
            }
        }
        if(list.size()!=0){
            detailVo.setSpecificationEntity(list,name);
        }

        //赋值userHasCollect
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.createCriteria().andValueIdEqualTo(id);
        List<MarketCollect> collects = marketCollectMapper.selectByExample(marketCollectExample);
        if(collects.size()>0){
            detailVo.setUserHasCollect(1);
        }else {
            detailVo.setUserHasCollect(0);
        }

        //给IssueEntity赋值
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        List<MarketIssue> issues = marketIssueMapper.selectByExample(marketIssueExample);
        for (MarketIssue issue : issues) {
            detailVo.setIssueEntity(issue);
        }

        //给CommentEntity赋值
        MarketCommentExample commentExample = new MarketCommentExample();
        commentExample.createCriteria().andValueIdEqualTo(id);
        PageHelper.startPage(1,10);
        List<MarketComment> comments = marketCommentMapper.selectByExample(commentExample);
        PageInfo pageInfo = new PageInfo(comments);
        detailVo.setCommentEntity(comments, (int) pageInfo.getTotal());

        //赋值attribute
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        marketGoodsAttributeExample.createCriteria().andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> goodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
        detailVo.setAttribute(goodsAttributes);

        //赋值InfoEntity
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        detailVo.setInfoEntity(marketGoods);

        //赋值BrandEntity
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(marketGoods.getBrandId());
        detailVo.setBrandEntity(marketBrand);

        //赋值ProductListEntity
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        productExample.createCriteria().andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> goodsProducts = marketGoodsProductMapper.selectByExample(productExample);
        for (MarketGoodsProduct goodsProduct : goodsProducts) {
            detailVo.setProductListEntity(goodsProduct);
        }

        return detailVo;
    }

    @Override
    public WXGoodsRelatedVo getWXGoodsRelated(Integer goodsId) {
        WXGoodsRelatedVo relatedVo = new WXGoodsRelatedVo();
        //由于相关关联的关系是商品id的前四位相同，
        // 故取前四位后面补0，取商品id大于该商品id小于前四位加1补0之间的商品
        Integer idMax = goodsId + 1000;
        String goodsMin = String.valueOf(goodsId);
        String goodsMax = String.valueOf(idMax);
        String headMin = goodsMin.substring(0, 4);
        String headMax = goodsMax.substring(0,4);
        Integer minId = Integer.parseInt(headMin + "000");
        Integer maxId = Integer.parseInt(headMax + "000");

        //查找id大于等于minId且小于maxId的商品
        PageHelper.startPage(1,6);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andIdBetween(minId,maxId);
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExample);
        PageInfo pageInfo = new PageInfo(marketGoodsList);
        relatedVo.setLimit(6);
        relatedVo.setPage(1);
        relatedVo.setTotal(pageInfo.getPages());
        relatedVo.setPages(pageInfo.getPages());

        for (MarketGoods marketGoods : marketGoodsList) {
            relatedVo.setListEntity(marketGoods);
        }
        return relatedVo;
    }

    private MarketGoodsAttribute getMarketGoodsAttributeByGoodsCreateBO(MarketGoods marketGoods,
                                                                        AdminGoodsCreateBO.AttributesDTO attribute) {
        MarketGoodsAttribute marketGoodsAttribute = new MarketGoodsAttribute();
        marketGoodsAttribute.setGoodsId(marketGoods.getId());
        marketGoodsAttribute.setAttribute(attribute.getAttribute());
        marketGoodsAttribute.setValue(attribute.getValue());
        Date date = new Date(System.currentTimeMillis());
        marketGoodsAttribute.setAddTime(date);
        marketGoodsAttribute.setUpdateTime(date);
        marketGoodsAttribute.setDeleted(false);
        return marketGoodsAttribute;
    }

    private MarketGoodsProduct getMarketGoodsProductByGoodsCreateBO(MarketGoods marketGoods,
                                                                    AdminGoodsCreateBO.ProductsDTO product) throws Exception {
        MarketGoodsProduct marketGoodsProduct = new MarketGoodsProduct();
        marketGoodsProduct.setGoodsId(marketGoods.getId());
        marketGoodsProduct.setSpecifications(product.getSpecifications());
        BigDecimal value = exchangeStringToBigDecimal(product.getPrice());
        marketGoodsProduct.setPrice(value);
        int number = 0;
        number = Integer.parseInt(product.getNumber());
        marketGoodsProduct.setNumber(number);
        marketGoodsProduct.setUrl(product.getUrl());
        Date date = new Date(System.currentTimeMillis());
        marketGoods.setAddTime(date);
        marketGoods.setUpdateTime(date);
        marketGoods.setDeleted(false);

        return marketGoodsProduct;
    }

    private MarketGoodsSpecification getGoodsSpecificationByGoodsCreateBO(MarketGoods marketGoods,
                                                                          AdminGoodsCreateBO.SpecificationsDTO specification) {
        MarketGoodsSpecification templeSpec = new MarketGoodsSpecification();
        templeSpec.setGoodsId(marketGoods.getId());
        templeSpec.setSpecification(specification.getSpecification());
        templeSpec.setValue(specification.getValue());
        templeSpec.setPicUrl(specification.getPicUrl());
        Date date = new Date(System.currentTimeMillis());
        templeSpec.setAddTime(date);
        templeSpec.setUpdateTime(date);
        templeSpec.setDeleted(false);
        return templeSpec;
    }

    private MarketGoods getMarketGoodsByGoodsCreateBO(AdminGoodsCreateBO.GoodsDTO newGoods) throws Exception {
        MarketGoods marketGoods = new MarketGoods();
        marketGoods.setGoodsSn(newGoods.getGoodsSn());
        marketGoods.setName(newGoods.getName());
        marketGoods.setCategoryId(newGoods.getCategoryId());
        marketGoods.setBrandId(newGoods.getBrandId());
        marketGoods.setGallery(newGoods.getGallery());
        marketGoods.setKeywords(newGoods.getKeywords());
        marketGoods.setBrief(newGoods.getBrief());
        marketGoods.setIsOnSale(newGoods.getIsOnSale());
        marketGoods.setPicUrl(newGoods.getPicUrl());
        // marketGoods.setShareUrl(newGoods.get);
        marketGoods.setIsNew(newGoods.getIsNew());
        marketGoods.setIsHot(newGoods.getIsHot());
        marketGoods.setUnit(newGoods.getUnit());
        BigDecimal value = exchangeStringToBigDecimal(newGoods.getCounterPrice());
        marketGoods.setCounterPrice(value);
        Date date = new Date(System.currentTimeMillis());
        marketGoods.setAddTime(date);
        marketGoods.setUpdateTime(date);
        marketGoods.setDeleted(false);
        marketGoods.setDetail(newGoods.getDetail());
        return marketGoods;
    }

    private BigDecimal exchangeStringToBigDecimal(String price) throws Exception {
        double doublePrice = 0;
        doublePrice = Double.parseDouble(price);
        return BigDecimal.valueOf(doublePrice);
    }
}
